<template>
  <div class="dashboard-container">
    <div class="app-container">
      <h2>
        <el-card class="tree-card">
          <tree-tools :tree-node="company" :is-root="true" />
          <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
            <!-- 顺序一定是 执行slot-scope的赋值 才去执行 props的传值 -->
            <tree-tools slot-scope="obj" :tree-node="obj.data" @delDepts="getDepartments" @addDepts="addDepts" @editDepts="editDepts" />
            <!-- <tree-tools slot-scope="obj" :tree-node="obj.data" @delDepts="getDepartments" @editDepts="editDepts" /> -->
          </el-tree>
        </el-card>

      </h2>
      <!-- 放置新增弹层组件  -->
      <add-dept ref="addDept" :show-dialog.sync="showDialog" :tree-node="node" @addDepts="getDepartments" />
    </div>
  </div>
</template>

<script>
import treeTools from './components/tree-tools.vue'
import AddDept from './components/add-dept' // 引入新增部门组件
import { getDepartments } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  components: {
    treeTools,
    AddDept
  },
  data() {
    return {
      showDialog: false, // 显示窗体
      node: {},
      company: { },
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    async getDepartments() {
      const result = await getDepartments()
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeData(result.depts, '')
    },
    addDepts(node) {
      this.node = node
      this.showDialog = true
    },
    editDepts(node) {
      // 首先打开弹层
      this.showDialog = true
      this.node = node // 赋值操作的节点
      // 父组件 调用子组件的方法
      this.$refs.addDept.getDepartDetail(node.id) // 直接调用子组件中的方法 传入一个id
    }
  }
}
</script>

<style>
.tree-card {
  padding: 30px  140px;
  font-size:14px;
  font-weight: normal;
}
</style>
